<?php
namespace jy_jindoufee;
defined('BY_JYA') or exit('error');
/*
	分佣插件工具类
*/
class model extends \plugin{
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function bankup(){
		if($this->setting['become_reg']==1){
			$member=$this->get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$member['realname'] || !$member['mobile']){
				return 0;
			}
			return 1;
		}
		return 1;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function up_del_day(){
		$del_day=intval($this->setting['del_day'])?:0;
		if(!$del_day){
			return false;
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$enddaytime=$daytime-$del_day*24*3600;
		$lastdaytime=$enddaytime-$del_day*24*3600;
		$members = pdo_getall('core_members',array('uniacid'=>$_SESSION['uniacid'],'credit2'=>0,'is_virtual'=>0,'mobile <>'=>'','createtime between'=>array($lastdaytime,$enddaytime)),array('id'));
		foreach($members as $lv){
			$ispay=pdo_count('jy_jindoufee_order',array('mid'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
			if(!$ispay){
				pdo_delete('core_members',array('id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
				pdo_delete('jy_jindoufee_member',array('mid'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return false;
	}
	public function gored($city=''){
		if(!$this->setting['is_redopen']){
			return $red;
		}
		$pindex = max(1, intval($_RQ['page']));
		$psize = 10;
		$total='';
		$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>array(6,7),'status'=>4);
		$logs=pdo_getslice('jy_jindoufee_commission',$params,array($pindex,$psize),$total,$fields,'','id desc');
		$tips='';
		foreach($logs as $lv){
			$nickname=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']), 'nickname');
			$time=date('Y-m-d H:i:s',$lv['complete_time']);
			$tips .=$nickname.'[领取了红包],时间:'.$time.']；';
		}
		$red['tips']=$tips;
		$red['getrednum']=pdo_count('jy_jindoufee_commission',array('type'=>array(6,7),'status'=>4,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$red['mygetallfee']=pdo_getsum('jy_jindoufee_commission',array('type'=>array(6,7),'status'=>4,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
		$red['lastrednum']=pdo_count('jy_jindoufee_commission',array('type'=>array(6,7),'status'=>3,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$red['lastrednum']=$red['lastrednum']?:0;
		$red['lastfee']=pdo_getsum('jy_jindoufee_commission',array('type'=>array(6,7),'status'=>3,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
		return $red;
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function upyuelog($data=''){
		$yuelog = pdo_get('jy_jindoufee_yuelog',array('uniacid'=>$_SESSION['uniacid'],'year'=>$data['year'],'month'=>$data['month'],'week'=>$data['week'],'day'=>$data['day']));
		$month=$data['day']?:$month;
		if($yuelog['id']){
			exi($month.'已发放奖励！','error');
		}
		$levels=pdo_getall('jy_jindoufee_level',array('uniacid'=>$_SESSION['uniacid']));
		foreach($levels as $lv){
			$this->upyuelogone($data,$lv);
		}
		return true;
	}
	public function upyuelogone($data=array(),$lv=array()){
		$members=pdo_getall('jy_jindoufee_member',array('is_agent'=>1,'ggfee >'=>0,'uniacid'=>$_SESSION['uniacid'],'level_id OR'=>'isallggsf=1 or level_id='.$lv['id']),array('mid','ggfee'));
		$allred=0;
		$num=count($members);
		$orderggfee=$this->getonelevelfee($lv,$data['starttime'],$data['endtime']);
		$fee=$num>0?$orderggfee/$num:0;
		if($num){
		    //print_r($orderggfee.'~~'.$fee);exit;
		}
		foreach($members as $item){
			$isadd=pdo_get('jy_jindoufee_feelog',array('type'=>4,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$data['year'].$data['month'].$data['week'].$data['day']));
			if($isadd['id']){
				//pdo_update('jy_jindoufee_feelog',array('createtime'=>$data['endtime'],'order_price'=>$order_price,'fee'=>$fee),array('id'=>$isadd['id'],'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$canfee=$fee>$item['ggfee']?$item['ggfee']:$fee;
				if($canfee<=0){
					continue;
				}
				
				$feelog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid'],
					'ordersn'=>$data['year'].$data['month'].$data['week'].$data['day'],
					'fee'=>$canfee,
					'createtime'=>$data['endtime'],
					'status'=>1,
					'type'=>4,
				);
				pdo_insert('jy_jindoufee_feelog',$feelog);
				pdo_update('jy_jindoufee_member',array('ggfee -='=>$canfee),array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid'],
					'ordersn'=>$data['year'].$data['month'].$data['week'].$data['day'],
					'money'=>$canfee,
					'type'=>4,
					'createtime'=>TIMESTAMP,
					'status'=>2,
				);
				$this->insert('commission',$new_commission);
				model('core/member')->member_credit($item['mid'],'credit3',$canfee,true,'jy_jindoufee','广告值释放金豆');
			}
		}
		$type=$data['week']?0:1;
		$type=$data['day']?2:$type;
		$yuelog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'level_id'=>$lv['id'],
				'num'=>$num,
				'allfee'=>$data['orderfee'],
				'allred'=>$orderggfee,
				'year'=>$data['year'],
				'month'=>$data['month'],
				'week'=>$data['week'],
				'day'=>$data['day'],
				'type'=>$type,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_jindoufee_yuelog',$yuelog);
	}
	public function upfeelog($mid=0,$fee='',$type=0,$status=0){
		$mid=$mid?:$_SESSION['uid'];
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'fee'=>$fee,
			'createtime'=>TIMESTAMP,
			'status'=>$status,
			'type'=>$type,
		);
		pdo_insert('jy_jindoufee_feelog',$feelog);
		return true;
	}
	public function getonelevelfee($level=array(),$starttime=0,$endtime=0){
		$orderlist=pdo_getall('jy_jindoufee_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime between'=>array($starttime,$endtime)));
		$onelevelfee=0;
		foreach($orderlist as $ov){
			$order_price=$ov['price'];
			$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$ov['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
			$ov['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),array('id','orderid','goodsid','total_price'));
			$this->needorder=false;
			foreach($ov['goods'] as $goods){
					if(!$goods['id']){
						$this->needorder=true;
						continue;	
					}
					$total_price=$goods['total_price'];
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$goods_commission['id']){
						$this->needorder=true;
						continue;
					}
					$order_price=$order_price-$total_price;
					if($goods_commission['ignore']){
						continue;
					}
					$goods_commission['commissions']=json_decode($goods_commission['commissions'],true);
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']!=$level['id']){
							continue;
						}else{
							$goods_sfgg_num=$gc['sfgg_num'];
							break;
						}
					}
					if($goods_sfgg_num && $total_price){
						if(substr($goods_sfgg_num,-1)=='%'){
							$onelevelfee +=substr($goods_sfgg_num,0,-1)*$total_price/100;
						}else{
							$onelevelfee +=$goods_sfgg_num;
						}
					}
			}
			if($this->needorder || count($ov['goods'])<=0){
				if($level['sfgg_num'] && $order_price){
					$onelevelfee +=$level['sfgg_num']*$order_price;
				}
			}
			//print_r($goods_sfgg_num.'~'.$total_price.'~~'.$onelevelfee);exit;
		}
		//print_r($goods_sfgg_num.'~'.$total_price.'~~'.$onelevelfee);exit;
		return $onelevelfee;
	}
}